﻿window.attachEvent('onunload', function(e) {
	e=e?e:event;
	var win = $fTopWindow();
	if (((e.clientX+20)>win.document.body.clientWidth && e.clientY<0) || e.altKey){ 
		win.location='/app/logout/close.do';
	}
});
/* action */
//提交表单
function doSubmit(url){
	//todo loading...
	if(url){
		document.forms[0].action=url;
	}
	document.forms[0].submit();
}
/*
 * 不提交表单，异步
 * url：Action的URL路径，例如“/app/a/a.do”，可以在“?”后设置参数
 * id：HTML对象的id属性值
 * domain：域
 * unitId：单元编号
 */
function doAjax(url,id,domain,unitId){
	$fAjax(true,null,url,id,domain,unitId);
}
//提交表单，异步
function doAjaxSubmit(url,id,domain,unitId){
	$fAjax(true,document.forms[0],url,id,domain,unitId);
}
//不提交表单，同步
function doSyncAjax(url,id,domain,unitId){
	$fAjax(false,null,url,id,domain,unitId);
}
//提交表单，同步
function doSyncAjaxSubmit(url,id,domain,unitId){
	$fAjax(false,document.forms[0],url,id,domain,unitId);
}
function doLink(url){
	if(url){
		window.location=url;
	}
}
function doListDelete(url,unitId){
	var table = $fGrid(unitId);
	if(!table){
		alert('[doListDelete] invalid unitId : '+unitId);
		return;
	}
	var action = false;
  var selects =$(table).getElements('input[name=_s_'+unitId+']');
	for(var i=0;i<selects.length;i++){		
		if(selects[i].value=='1'){
			action = true;
			break;
		}
	}
	if (action && confirm('删除操作不可恢复，你确认吗？')){
		_doRemoveSelected(unitId,false);
		document.forms[0].action=url;
		doSubmit();
	}
}
function doFormDelete(url,unitId){
	var table = $fGrid(unitId);
	if(!table){
		alert('[doFormDelete] invalid unitId : '+unitId);
		return;
	}
  var operators = table.getElements('input[name='+unitId+'.]');
	if (operators && operators.length==1 && confirm('删除操作不可恢复，你确认吗？')){
		operators[0].value=3;
		document.forms[0].action=url;
		doSubmit();
	}
}
function doListAdd(unitId){
	var table = $fGrid(unitId);
	if(!table){
		alert('[doListAdd] invalid unitId : '+unitId);
		return;
	}
	var insertRow = $($fTable(table.parentNode)).getElement('textarea[id=_in_]');
	if(insertRow){
		table.parentNode.innerHTML = table.outerHTML.substring(0,table.outerHTML.length-8)+insertRow.value +'</table>';
		var grid = new Grid(unitId);
		grid.setId();
		grid.setIndex();
		$fResetCheck(unitId);
	}
}
function doFormAdd(unitId){
	var wrapper = $fWrapper(unitId);
	if(!wrapper){
		alert('[doFormAdd] invalid unitId : '+unitId);
		return;
	}
	var insertRow = wrapper.getElement('textarea[id=_in_]');
	if(insertRow){
		wrapper.parentNode.innerHTML = wrapper.outerHTML.substring(0,wrapper.outerHTML.length-8)+insertRow.value +'</table>';
	}
}
function doRemove(unitId){
	_doRemoveSelected(unitId,true);
}
/* selectors */
function $fUnit(unitId){
	return $('u_u_'+unitId);
}
function $fGrid(unitId){
	return $('u_g_'+unitId);
}
function $fWrapper(unitId){
	return $('u_w_'+unitId);
}

/* system */
var $fSwitchIcon={
	Minus:'/common/images/unit/minus.gif',
	Plus:'/common/images/unit/plus.gif'
};
function $fSwitch(obj){
	var table = $fTable(obj);
	if(!table.id || table.id.indexOf('u_u_')<0){
		table = $fTable(table.parentNode);
	}
	var show ;
	if(obj.src.indexOf($fSwitchIcon.Minus)>=0){
		obj.src =$fSwitchIcon.Plus;
		show = 'none';
	}else{
		obj.src =$fSwitchIcon.Minus;
		show = 'block';
	}
	var rows = table.rows;
	for(var i=1;i<rows.length;i++){
		rows[i].style.display=show;
	}
}
function $fLogin(){
	$fTopWindow().location='/app/login.do';
}
function $fSelectAll(unitId,obj){
	var table=$fTable(obj);
  var selects = $(table).getElements('input[name=_s_'+unitId+']');
	for (var i=0;i<selects.length;i++){
		selects[i].value = (obj.checked?'1':'0');
		selects[i].parentNode.childNodes[1].checked=obj.checked;
	}
}
function $fResetCheck(unitId){
	var obj = $fGrid(unitId).getElement('input[id=_sa_]');
	if(obj){
		var grid = new Grid(unitId);
		obj.checked=grid.allSelected() && !grid.allInvisible() ;
	}
}
function _doRemoveSelected(unitId,makeInvisible){
	var table = $fGrid(unitId);
  var selects = $(table).getElements('input[name=_s_'+unitId+']');
	var operators = $(table).getElements('input[name='+unitId+'.]');
	if(selects.length==0 || selects.length!=operators.length){
		return;
	}
	var grid = new Grid(unitId);
	for (var i=0;i<selects.length;i++){
		grid.groups[i].doRemove(selects[i],operators[i],makeInvisible);
	}
	grid = new Grid(unitId);
	grid.setIndex();
	$fResetCheck(unitId);
}
function _showCard(groupId){
	var tab = $fTd(event.srcElement).getElementsByTagName('DIV')[0];
	if(tab.className=='C1'){
		return;
	}
	var cardWrapper = _getCardWrapper(event.srcElement);
	_showCardWithWrapper(cardWrapper,groupId);
}
function _showCardWithWrapper(wrapper,groupId){
	var unitId = wrapper.id.substring(4,wrapper.id.length);
	var tabsTable = wrapper.all('tabs_'+unitId);
	if(tabsTable){
		var tabs = tabsTable.getElementsByTagName('DIV');
		for(var i=0;i<tabs.length;i++){
			if(tabs[i].id){
				tabs[i].className = (tabs[i].id==groupId?'C1':'C0');
			}
		}
	}
	var rows = wrapper.all('u_g_'+unitId).rows;
	for(var i=0;i<rows.length;i++){
		rows[i].style.display = ((!rows[i].id || rows[i].id==groupId)?'block':'none');
	}	
}
function _showGroup(groupId){
	var grid = _getGridWithoutId()
	_showGroupWithGrid(grid,groupId);
}
function _showGroupWithGrid(grid,groupId){
	var group = new Group(grid.rows,groupId);
	group.show();
}
/* navigator */
function _go(currentPageNumber,maxPageNumber){
	var cell = $fTd(event.srcElement);
	var fields =  cell.getElementsByTagName('INPUT');
	var pageNumber = fields[1].value;
	if(!_isInt(pageNumber) || parseInt(pageNumber)<1 || parseInt(pageNumber)>maxPageNumber || parseInt(pageNumber)==currentPageNumber){
		fields[1].value = '';
		return ;
	}else{
		fields[0].value = pageNumber;
		doSubmit();
	}
}
function _to(pageNumber){
	var cell = $fTd(event.srcElement);
	var fields =  cell.getElementsByTagName('INPUT');
	fields[0].value = pageNumber;
	doSubmit();
}
/* Grid */
function Grid(unitId){
	this.id = unitId;
	this.index = 1;
	this.groups = new Array();
	var rows = $fGrid(unitId).getElements('tr');
	if(!rows){return;}
	var lastRowId='x';
	var rowId;
	for (var i=0;i<rows.length ;i++)	{
		rowId = rows[i].id;
		if(rowId && rowId!=lastRowId){this.groups[this.groups.length] = new Group(rows,rowId,this);}
		lastRowId = rowId;
	}
}
Grid.prototype.getIndex = function(){
	return this.index++;
}
Grid.prototype.setId = function(){
	for (var i=0;i<this.groups.length ;i++)	{ this.groups[i].setId(i);}
}
Grid.prototype.setIndex = function(){
	for (var i=0;i<this.groups.length ;i++)	{ this.groups[i].setIndex(); }
}
Grid.prototype.allSelected = function(){
	for (var i=0;i<this.groups.length ;i++)	{
		if(!this.groups[i].selected()){return false;}
	}
	return true;
}
Grid.prototype.allInvisible = function(){
	for (var i=0;i<this.groups.length ;i++)	{
		if(!this.groups[i].invisible()){return false;}
	}
	return true;
}
function Group(rows,rowId,parent){
	this.id = rowId;
	this.parent = parent;
	this.rows= new Array();
	var row;
	for (var i=0;i<rows.length ;i++)	{
		row = rows[i];
		if(row.id==rowId){this.rows.push(row);}
	}
}
Group.prototype.setCss = function(css){
	for (var i=0;i<this.rows.length ;i++)	{this.rows[i].className=css;}
}
Group.prototype.setId = function(rowId){
	for (var i=0;i<this.rows.length ;i++)	{ this.rows[i].id=rowId; }
}
Group.prototype.setIndex = function(){
	var indexCell = this.rows[0].all('_i_');
	if(indexCell && indexCell.parentNode.style.display !='none'){
		indexCell.innerHTML=this.parent.getIndex();
	}
}
Group.prototype.doRemove = function(select,operator,makeInvisible){
	if(select.value=='1'){
		if(operator.value=='2' || operator.value=='0'){
			operator.value = '3';
			if(makeInvisible){this.show(false);}
		}else if(operator.value=='1'){
			this.remove();
		}
	}
}
Group.prototype.show = function(){
	if(this.rows.length>0){
		var show = this.rows[0].style.display=='none';
		for (var i=0;i<this.rows.length ;i++)	{this.rows[i].style.display=show?'block':'none';}
	}
}
Group.prototype.remove = function(){
	for (var i=0;i<this.rows.length ;i++)	{this.rows[i].removeNode(true);}
}
Group.prototype.selected = function(){
	var s = this.rows[0].all('_s_'+this.parent.id);
	return s?s.parentNode.childNodes[1].checked:true;
}
Group.prototype.invisible = function(){
	return this.rows[0].style.display=='none';
}
Group.prototype.getIndex = function(cell){
	for (var i=0;i<this.rows.length ;i++)	{
		var cells = this.rows[i].cells;
		for (var j=0;j<cells.length ;j++)	{
			if(cells[j]==cell){return new Array(i,j);}
		}
	}
	return null;
}
Group.prototype.getValue = function(index,name){
	var cell = this.rows[index[0]].cells[index[1]];
	var obj = cell.all(name);
	if(obj){
		return obj.value;
	}else{
		return cell.innerHTML;
	}
}
function _getGridWithoutId(obj){
	var parent=obj?obj.parentNode:event.srcElement.parentNode;	
	while((parent.tagName!='TABLE' || !parent.id || parent.id.indexOf('u_g_')!=0) && parent.parentNode){
		parent = parent.parentNode;
	}
	return parent;
}
function _getCardWrapper(obj){
	var parent=obj.parentNode;	
	while((parent.tagName!='TABLE' || !parent.id || parent.id.indexOf('u_c_')!=0) && parent.parentNode){
		parent = parent.parentNode;
	}
	return parent;
}
/* sort */
var $fSortIcon={
	None:'/common/images/list/s0.png',
	Up:'/common/images/list/s1.png',
	Down:'/common/images/list/s2.png'
};
function $fSort(cell,name,type){
	var img = cell.getElementsByTagName('IMG')[0];
	//set img and desc
	var desc =false;
	if(img.src.indexOf($fSortIcon.None)>0){
		img.src = $fSortIcon.Up;
	}else if(img.src.indexOf($fSortIcon.Up)>0){
		img.src = $fSortIcon.Down;
		desc = true;
	}else if(img.src.indexOf($fSortIcon.Down)>0){
		img.src = $fSortIcon.Up;
	}	
	//set index
	var gridTable = $fTable(cell);
	var unitId = gridTable.id.substring(4,gridTable.id.length);
	var head = new Group(gridTable.rows,'x');
	var index = head.getIndex(cell);
	//sorting
	var grid = new Grid(unitId);
	var groups = grid.groups;
	groups.sort(_compareGroup(name,type,desc,index));
	//set index
	grid.setIndex();
	//updating
	var tBody = gridTable.tBodies[0];
	for (var i = 0; i < groups.length; i++) {
		var rows = groups[i].rows;
		for (var j = 0; j < rows.length; j++) {
			tBody.appendChild(rows[j]);
		}
	}
}
function _compareGroup(name,type,desc, index) {
	var formatter = String;	
	if (type == '1'){
		formatter = Number;
	}else if (type == '3'){
		formatter = function (s) {return s.toUpperCase();};
	}else if (type == '4'){
		formatter = function (s) {return Date.parse(s.replace(/\-/g, '/'));};
	}
	return function (group1, group2) {
		var value1 = formatter(_getCellValue(group1.rows[index[0]].cells[index[1]],name));
		var value2 = formatter(_getCellValue(group2.rows[index[0]].cells[index[1]],name));
		if(value1<value2){
			return desc ? 1 : -1;
		}else if(value1>value2){
			return desc ? -1 : 1;
		}else{
			return 0;
		}
	};
}
function _getCellValue(cell,name) {
	var obj = cell.all(name);
	return obj?obj.value:cell.innerText;
}
/* mark row (todo: using tr class is fast?)*/
var _lastOverRowId ;
var _lastOverRowClassName ;
var _lastSelectedRowId ;
var _lastSelectedRowClassName ;
//on row over
function $fRO(event){
	var row = $fTr(event.srcElement);
	if(!_isValidRow(row) || row.id==_lastSelectedRowId){
		return;		
	}
	_lastOverRowId = row.id;
	_lastOverRowClassName = row.className;
	var group = new Group($fTable(row).rows,row.id);
	group.setCss('RO');
}
//on row out
function $fRX(event){
	var row = $fTr(event.srcElement);
	if(!_isValidRow(row) || !_lastOverRowId || _lastOverRowId ==_lastSelectedRowId){
		return;
	}
	var group = new Group($fTable(row).rows,_lastOverRowId);
	group.setCss(_lastOverRowClassName);
}
//on row click
function $fRC(event){
	var row = $fTr(event.srcElement);
	if(!_isValidRow(row) || row.id==_lastSelectedRowId){
		return;
	}
	var rows = $fTable(row).rows;
	if(_lastSelectedRowId){
		var group = new Group(rows,_lastSelectedRowId);
		group.setCss(_lastSelectedRowClassName);
	}
	_lastSelectedRowId = row.id;
	//_lastOverRowClassName will be used while move mark enabled
	_lastSelectedRowClassName =_lastOverRowClassName || row.className;
	var group = new Group(rows,row.id);
	group.setCss('RS');
}
function _isValidRow(row){return  row.id && row.id!='x';}
/* components */
//MultiSelectField
function $f41(src){
	var obj =$(src).getPrevious();
	var items = $(src).getElements('option');
	var value ='';
	for (var i=0;i<items.length ;i++)	{if(items[i].selected){value += ','+items[i].value;}}
	obj.value = value.length>0?value.substring(1,value.length):'';
}
//SelectField
function $f43(showAll,obj,height){
  var id = obj.id.substring(0,obj.id.length-3);
  $f43Remove(id,obj.parentNode);
  var items = $(id+'_43src').getElements('option');
  var html = '';
  for(var i=0;i<items.length;i++){
    if(showAll || items[i].text.indexOf(obj.value)==0){html+='<div onclick=$f43Click("'+id+'",this) title="'+items[i].value+'" '+(items[i].text==obj.value?'class="E43S"':'onmouseover="$f43OnOver(this)" onmouseout="$f43OnOut(this)"')+'>'+items[i].text+'</div>';}
  }
  if(html.length>0){
	  var c = document.createElement('span');
	  c.id=id+'_43c';
	  c.className='E43C';
	  c.innerHTML=html.substring(0,html.length-6);
    $(c).setStyles({overflow:'auto',position: 'absolute'});
	  obj.parentNode.appendChild(c);
    var oc = $(obj).getCoordinates();
    var os = $(obj).getSize();
    var oss = $(obj).getScrollSize();
	  var cHeight=$(c).getCoordinates().height;
	  if(height && height<cHeight){$(c).setStyles({height:height});cHeight = height;}
	  var top = ($(document.body).getSize().y+$(document.body).getScroll().y<oc.top+cHeight)?(oc.top-cHeight+(oss.y-os.y)):(oc.top+oss.y);
    $(c).setStyles({left:oc.left+oss.x-os.x,top:top,width:oc.width});//定位
  }
}
function $f43Close(obj){
  var id = obj.id.substring(0,obj.id.length-3);
  if($(id+'_43src').title!='y') return;
  var c = $(obj.parentNode).getElement('span[id='+id+'_43c]');
  var item = null;
  if(c && obj.value && obj.value.length>0){
    var items = c.getElements('div');
	for(var i=0;i<items.length;i++){if(items[i].innerHTML==obj.value){item=items[i];}}
	if(!item){item = items[0];}
  }
  $f43Set(id,obj.parentNode,item);
  if(c){c.parentNode.removeChild(c);}
}
function $f43Click(id,obj){var p = obj.parentNode.parentNode;$f43Set(id,p,obj);$f43Remove(id,p);}
function $f43Remove(id,p){var c = $(p).getElement('span[id='+id+'_43c]');if(c){c.parentNode.removeChild(c);}}
function $f43Set(id,p,item){$(p).getElement('input[name='+id+']').value=item?item.title:'';$(p).getElement('input[id='+id+'_43]').value=item?item.innerHTML:'';}
function $f43OnOver(obj){obj.className='E43O';}
function $f43OnOut(obj){obj.className='';}
function $f43Out(id,out){$(id+'_43src').title=out;}
//RadioField
function $f46(src){
	var objs = $(src).getElements('input');
	for (var i=1;i<objs.length ;i++)	{
		if(objs[i].checked){
		  objs[0].value = objs[i].value;
			break;
		}
	}
	return objs[0];
}
//GlobalRadioField
function $f48(src){
	var obj = $f46(src);
	objs = $$('input[name='+obj.name+']');
	for (var i=0;i<objs.length ;i++)	{
		if(objs[i]!=obj){objs[i].value='';}
	}
}
//CheckBoxField
function $f52(src){var objs = src.childNodes;objs[0].value = objs[1].checked?'1':'0';}
//CheckBoxSetField
function $f53(src){
	var objs = $(src).getElements('input');
	var value ='';
	for (var i=1;i<objs.length ;i++)	{
		if(objs[i].checked){value += ','+objs[i].value;}
	}
	objs[0].value = value.length>0?value.substring(1,value.length):'';
}
//todo tip
function $f70(str){
	alert(str);
}
/* Validator */
function _isInt(value) {  
  var integer = parseInt(value); 
  return !isNaN(integer) && integer.toString().length==value.length ;
} 
/* util */
function $fAjax(async,data,url,id,domain,unitId){
	var request;
	if(id && domain && unitId){
		url+=((url.indexOf('?')>0?'&ajaxd=':'?ajaxd=')+domain+'&ajaxu='+unitId);
	}
	if(id){
		request = new Request.HTML({url:url,async:async,update:$(id)});
	}else{
		request = new Request.HTML({url:url,async:async});
	}
	data?request.post(data):request.post();
}
function $fPrint(){var bar = $('PrintBar');if(bar){bar.style.display='none';window.setTimeout('window.print();',1);window.setTimeout("$('PrintBar').style.display = 'block';",1);}else{window.print();}}
function $fLink(link){
	var index = link.indexOf('.');
	var domain = link.substring(0,index);
	var id = link.substring(index+1,link.length);
	var url = '/app/common/link.do?domain='+domain+'&id='+id;
	doSyncAjaxSubmit(url,id);
	var html = $(id).innerHTML;
	index = html.indexOf('$fLink(');
	if(index>0){
		link = html.substring(index+8,html.length);
		link = link.substring(0,link.indexOf(')')-1);
		$fLink(link);
	}
}
function $fns(name,parent){return parent?$(parent).getElements('[name='+name+']'):$$('[name='+name+']');}
function $fn(name,parent){return $fns(name,parent)[0];}
function $fnv(name,parent){return $fn(name,parent).value;}
function $fTopWindow(){var obj = window;var i = 0;while(obj.parent && i<5){obj = obj.parent;i++;}return obj;}
/* find first parent */
function $fDiv(obj){return $fObj(obj,'DIV');}
function $fSpan(obj){return $fObj(obj,'SPAN');}
function $fTd(obj){return $fObj(obj,'TD');}
function $fTr(obj){return $fObj(obj,'TR');}
function $fTable(obj){return $fObj(obj,'TABLE');}
function $fObj(obj,tagName){
	if(obj.tagName==tagName){
		return obj;
	}
	var parent=obj.parentNode;	
	while(parent.tagName!=tagName && parent.parentNode){
		parent = parent.parentNode;
	}
	return parent.tagName==tagName?parent:null;
}
